<?php
// +----------------------------------------------------------------------
// | 商户模块
// +----------------------------------------------------------------------

namespace app\mall\model;

use think\Model as ThinkModel;
use util\Tree;
use think\facade\Db;

/**
 * 商户模型
 * @package app\mall\model
 */
class Merch extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mall_merch';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    /**
     * 获取商户列表
     * @return array|mixed
     */
    public static function getList()
    {
        $data_list = cache('merch_list');
        if (!$data_list) {
            $data_list = self::where('status', 1)->where('trash', 0)->order('sort')->paginate(config('paginate'));
            // 非开发模式，缓存数据
            if (config('app.develop_mode') == 0) {
                cache('merch_list', $data_list);
            }
        }
        return $data_list;
    }
	
	/**
	 * 获取商户简单列表
	 * @return array|mixed
	 */
	public static function getColumn($cid = 0)
	{
	    $data_list = cache('merch_column');
	    if (!$data_list || $cid > 0) {
			$map = [
				['status', '=', 1],
				['trash', '=', 0],
			];
			if ($cid > 0) $map[] = ['cid', '=', $cid];
	        $data_list = self::where($map)->order('sort')->column('short_name', 'id');
	        // 非开发模式，缓存数据
	        if (config('app.develop_mode') == 0 && $cid == 0) {
	            cache('merch_column', $data_list);
	        }
	    }
	    return $data_list;
	}
	
    /**
     * 获取指定商户数据
     * @param int $id id
     * @return mixed|static
     */
    public static function getInfo($id = 0)
    {
        $result = cache('merch_info_'. $id);
        if (!$result) {
            $result = self::where('id', $id)->where('status', 1)->where('trash', 0)->find();
            // 非开发模式，缓存数据
            if (config('app.develop_mode') == 0) {
                cache('merch_info_'. $id, $result);
            }
        }
        return $result;
    }
	
	/**
	 * 微信费率
	 * @param int $id 类型id
	 * @author paulxun
	 * @return array|static
	 */
	public static function getWxRate($id = 99)
	{
		$types = [
			'0.01'  => '1%',
			'0.006' => '0.6%',
			'0.003'  => '0.3%',
			'0.002'  => '0.2%',
			'0'  => '0%',
		];
	    if ($id === 99) {
	        $result = $types;
	    }else{
			$result = '';
			if(isset($types[$id])) $result = $types[$id];
		}
	    return $result;
	}
	
	/**
	 * 支付宝费率
	 * @param int $id 类型id
	 * @author paulxun
	 * @return array|static
	 */
	public static function getAliRate($id = 99)
	{
		$types = [
			'0.01'  => '10‰',
			'0.006' => '6‰',
			'0.0055'  => '5.5‰',
			'0.0038'  => '3.8‰',
			'0.003'  => '3‰',
			'0.002'  => '2‰',
		];
	    if ($id === 99) {
	        $result = $types;
	    }else{
			$result = '';
			if(isset($types[$id])) $result = $types[$id];
		}
	    return $result;
	}
}